/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.controller;


import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.actor.ServerModel;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.controller.exception.NullListException;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.controller.exception.ObjectNotFoundExeption;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.observer.Observer;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.observer.Subject;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.reference.ManagerReference;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.register.Register;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.util.Message;

/**
 *
 * @author usuario
 */
public class ServerControler extends Register implements ServerInterface, Serializable {

    private static Integer port = 1029;
    public static String address = "rmi://localhost:"+port.toString()+"/Server";
    public ServerModel server;
    private ManagerReference managerReference;
    
    public ServerControler()throws RemoteException, MalformedURLException {
        super(address, port);
        server = new ServerModel();
        managerReference = ManagerReference.getInstance();
    }


    
    public List<ChatControllerInterface> returnListToChatRooms() throws RemoteException, NullListException {

        if (!server.getChatList().isEmpty()) {
            return server.getChatList();
        } else {
            throw new NullListException();
        }
    }

 public void Notify(Message message) throws RemoteException {

       for(int i = 0; i < server.getChatList().size(); i++){
           Iterator<ClientInterface> clients = server.getChatList().get(i).getClients().iterator();

           while(clients.hasNext()){
               ClientInterface client = clients.next();
               client.update(message);
           }

       }

    }

    public void Attach(Remote o) throws RemoteException {
        try {
            int id;
            if (server.getChatList().isEmpty()) {
                id = 0;
            } else {
                //pega o valor do id do ultimo chat e soma 1
                id = server.getChatList().get(server.getChatList().size() - 1).getId() + 1;
            }
            ChatControllerInterface chat = (ChatControllerInterface) o;
            chat = (ChatControllerInterface) managerReference.getReference(ManagerReference.CHAT, chat.getURL());
            server.insertChat(chat);
            chat.setId(id);
            
            Message message = new Message();

            message.setCode(Message.ADD_CHAT);
            message.setContent(chat);

            Notify(message);

            listarChats();

        } catch (NotBoundException ex) {
            Logger.getLogger(ServerControler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ServerControler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ObjectNotFoundExeption ex) {
            Logger.getLogger(ServerControler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void listarChats(){
        for(int i = 0; i < server.getChatList().size(); i++){
            try {
                System.out.println(server.getChatList().get(i).getURL());
            } catch (RemoteException ex) {
                Logger.getLogger(ServerControler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void Detach(Remote o) throws RemoteException {
        server.getChatList().remove(o);

        Message message = new Message();

        message.setCode(Message.REMOVE_CHAT);
        message.setContent(server.getChatList());

        Notify(message);
    }

    public String getURL() throws RemoteException {
       return super.getUrl();
    }

    public void createRegister() throws RemoteException, MalformedURLException {
        super.register();

    }


}
